package com.erp.erp_servers.bill_exchange.Imp;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_dao.BillExchangeInfo.IBillExchangeInfoDao;
import com.erp.erp_entitys.BillExchangeInfo.BillExchangeCostInfo;
import com.erp.erp_entitys.BillExchangeInfo.BillExchangeInfo;
import com.erp.erp_entitys.BillExchangeInfo.Cost;
import com.erp.erp_entitys.BillExchangeInfo.Product;
import com.erp.erp_entitys.bill_declare.BillDeclareInfo;
import com.erp.erp_entitys.bill_declare.InfoSearch;
import com.erp.erp_entitys.bill_declare.carryInfoSearch;
import com.erp.erp_entitys.bill_declare.logo;
import com.erp.erp_entitys.business.entity.CarryDetailEntity;
import com.erp.erp_entitys.business.req.ContractStatisticsOneReq;
import com.erp.erp_entitys.business.rsp.ContractCustomerInfoRsp;
import com.erp.erp_servers.bill_exchange.BillExchangeInfoService;
import com.erp.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 结汇主信息表(BillExchangeInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-01-15 21:13:36
 */
@Service("billExchangeInfoService")
public class BillExchangeInfoServiceImpl implements BillExchangeInfoService {
    @Autowired
    private BillExchangeInfoService infoService;

    @Resource
    private IBillExchangeInfoDao billExchangeInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param billId 主键
     * @return 实例对象
     */
    @Override
    public BillExchangeInfo queryById(Integer billId) {
        return this.billExchangeInfoDao.queryById(billId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<BillExchangeInfo> queryAllByLimit(int offset, int limit) {
        return this.billExchangeInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param billExchangeInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BillExchangeInfo insert(BillExchangeInfo billExchangeInfo) {
        this.billExchangeInfoDao.insert(billExchangeInfo);
        return billExchangeInfo;
    }

    /**
     * 修改数据
     *
     * @param billExchangeInfo 实例对象
     * @return 实例对象
     */
    @Override
    public BillExchangeInfo update(BillExchangeInfo billExchangeInfo) {
        this.billExchangeInfoDao.update(billExchangeInfo);
        return this.queryById(billExchangeInfo.getBillId());
    }

    /**
     * 通过主键删除数据
     *
     * @param billId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer billId) {
        return this.billExchangeInfoDao.deleteById(billId) > 0;
    }

    @Override
    public List<BillExchangeInfo> selectAllInfobySearch(InfoSearch search) {
        return billExchangeInfoDao.selectAllInfobySearch(search);
    }

    @Override
    public List<Cost> selectCostbySearch(InfoSearch search) {
        return billExchangeInfoDao.selectCostbySearch(search);
    }

    @Override
    public List<Product> selectProductStaticbySearch(InfoSearch search) {
        return billExchangeInfoDao.selectProductStaticbySearch(search);
    }

    @Override
    public List<CarryDetailEntity> queryCarryBySearchExchange(carryInfoSearch search) {
        return billExchangeInfoDao.queryCarryBySearchExchange(search);
    }

    @Override
    public List<BillExchangeInfo> checkBillNo(String invoicenumber) {
        return billExchangeInfoDao.checkBillNo(invoicenumber);
    }

    @Override
    public logo selectLogo(Integer declare_id) {
        return billExchangeInfoDao.selectLogo(declare_id);
    }

    @Override
    public JSONObject getCostContractStatisticsOneInfo(InfoSearch search) {
        //获取费用数据
        List<Cost> infos = infoService.selectCostbySearch(search);
        for(Cost c:infos){
            try {
                if(c.getMoney_kind().equals("人民币")){
                    BigDecimal divide = null;
                    if(c.getUsd_rate().compareTo(BigDecimal.valueOf(1)) == 1){
                        divide= c.getCost_money().divide(c.getUsd_rate(), 2, BigDecimal.ROUND_HALF_UP);
                    }else{
                        divide=c.getCost_money().multiply(c.getUsd_rate()).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP);
                    }
                    c.setUsb(divide);
                    c.setRmb(c.getCost_money());
                }else{
                    //  return (element.cost_money*element.usd_rate).toFixed(2);
                    BigDecimal multiply = (c.getCost_money().multiply(c.getUsd_rate())).divide(BigDecimal.valueOf(1),2,BigDecimal.ROUND_HALF_UP);
                    c.setUsb(multiply);
                    BigDecimal divide = c.getCost_money().multiply(c.getUsd_rate()).multiply(c.getRmb_rate()).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP);
                    c.setRmb(divide);
                }
            }catch (Exception e){

            }
        }
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleInfoOfContractCustomer();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(infos)){
            obj.put("data", data);
            return obj;
        }
        List<Cost> voList = getContractCustomerInfo(infos);

        //封装数据
        getContractCustomerInfo(data, voList);

        obj.put("data", data);
        return obj;
    }


    @Override
    public JSONObject getProductContractStatisticsOneInfo(InfoSearch search) {
        //获取费用数据
        List<Product> infos = infoService.selectProductStaticbySearch(search);
        for(Product p:infos){
            try{
                if(p.getMoneyKind().equals("人民币")){
                    BigDecimal divide = null;
                    if(p.getUsd_rate().compareTo(BigDecimal.valueOf(1)) == 1){
                        divide= p.getFtotalprice().divide(p.getUsd_rate(), 2, BigDecimal.ROUND_HALF_UP);
                    }else{
                        divide=p.getFtotalprice().multiply(p.getUsd_rate()).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP);
                    }
                    p.setUsb(divide);
                    p.setRmb(p.getFtotalprice());
                }else{
                    BigDecimal divide = p.getFtotalprice().multiply(p.getUsd_rate()).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP);
                    p.setUsb(divide);
                    BigDecimal divide1 = p.getFtotalprice().multiply(p.getUsd_rate()).multiply(p.getRmb_rate()).divide(BigDecimal.valueOf(1), 2, BigDecimal.ROUND_HALF_UP);
                    p.setRmb(divide1);
                }}catch (Exception e){

            }
        }
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleInfoOfContractCustomer1();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(infos)){
            obj.put("data", data);
            return obj;
        }
        List<Product> voList = getContractCustomerInfo1(infos);

        //封装数据
        getContractCustomerInfo1(data, voList);

        obj.put("data", data);
        return obj;
    }
    /**
     * 获取excel的title信息
     * 结汇费用
     * @return excel的title信息
     */
    private JSONArray getTitleInfoOfContractCustomer(){
        JSONArray titleList = new JSONArray();
        titleList.add("发票号码");
        titleList.add("销售合同号");
        titleList.add("价格术语");
        titleList.add("币种");
        titleList.add("美元汇率");
        titleList.add("人民币汇率");
        titleList.add("金额小计");
        titleList.add("美元小计");
        titleList.add("人民币小计");
        titleList.add("费用描述");
        titleList.add("客户名");
        titleList.add("发票日期");
        titleList.add("业务员");
        return titleList;
    }
    /**
     * 获取excel的title信息
     * 结汇产品
     * @return excel的title信息
     */
    private JSONArray getTitleInfoOfContractCustomer1(){
        JSONArray titleList = new JSONArray();
        titleList.add("发票编号");
        titleList.add("发票日期");
        titleList.add("价格术语");
        titleList.add("业务模式");
        titleList.add("是否含税");
        titleList.add("月份");
        titleList.add("业务员");
        titleList.add("客户名称");
        titleList.add("客户来源");
        titleList.add("批次");
        titleList.add("产品编码");
        titleList.add("产品中文名称");
        titleList.add("规格编码");
        titleList.add("产品中文描述");
        titleList.add("成品成本");
        titleList.add("配件合计");
        titleList.add("成品单价");
        titleList.add("产品英文名称");
        titleList.add("产品英文描述");
        titleList.add("单价");
        titleList.add("币种");
        titleList.add("数量");
        titleList.add("单位");
        titleList.add("金额小计");
        titleList.add("人民币小计");
        titleList.add("美元小计");
        titleList.add("签约公司名称");
        titleList.add("配件采购单");
        titleList.add("配件供应商");
        titleList.add("产品采购单号");
        titleList.add("产品供应商名称");
        titleList.add("外销合同号");
        titleList.add("成本小计");
        titleList.add("配件成本");
        titleList.add("目的地");
        titleList.add("美元汇率");
        titleList.add("人民币汇率");
        return titleList;
    }
    /**
     * 封装费用数据
     * @param data 封装后数据
     * @param voList 原始数据
     */
    private void getContractCustomerInfo(JSONArray data, List<Cost> voList){
        for(Cost vo : voList){
            JSONArray infoList = new JSONArray();
            infoList.add(vo.getInvoice_no());
            infoList.add(vo.getContract_no());
            infoList.add(vo.getPrice_term());
            infoList.add(vo.getMoney_kind());
            infoList.add(vo.getUsd_rate());
            infoList.add(vo.getRmb_rate());
            infoList.add(vo.getCost_money());
            infoList.add(vo.getUsb());
            infoList.add(vo.getRmb());
            infoList.add(vo.getCost_desc());
            infoList.add(vo.getCustomer_name());
            infoList.add(vo.getInvoice_date());
            infoList.add(vo.getSalesman());
            data.add(infoList);
        }
    }
    /**
     * 封装产品数据
     * @param data 封装后数据
     * @param voList 原始数据
     */
    private void getContractCustomerInfo1(JSONArray data, List<Product> voList){
        for(Product vo : voList){
            JSONArray infoList = new JSONArray();
            infoList.add(vo.getInvoiceNo());
            //日期转换
            infoList.add(changeDate(vo.getInvoiceDate()));
            infoList.add(vo.getPriceTerm());
            infoList.add(vo.getBusinessMode());
            infoList.add(vo.getTaxIncluded());
            infoList.add(vo.getMon());
            infoList.add(vo.getSalesman());
            infoList.add(vo.getCustomerName());
            infoList.add(vo.getSource());
            infoList.add(vo.getLot());
            infoList.add(vo.getGoodsCode());
            infoList.add(vo.getGoodsCname());
            infoList.add(vo.getSpecCode());
            infoList.add(vo.getGoodsCdesc());
            infoList.add(vo.getFinishedProductCost());
            infoList.add(vo.getPartAllPlice());
            infoList.add(vo.getCostUnitPrice());
            infoList.add(vo.getGoodsEname());
            infoList.add(vo.getGoodsEdesc());
            infoList.add(vo.getFunitprice());
            infoList.add(vo.getMoneyKind());
            infoList.add(vo.getLastAmount());
            infoList.add(vo.getGoodsUnit());
            infoList.add(vo.getFtotalprice());
            infoList.add(vo.getRmb());
            infoList.add(vo.getUsb());
            infoList.add(vo.getCompanyName());
            infoList.add(vo.getPartOrder());
            infoList.add(vo.getPartCompany());
            infoList.add(vo.getProductStockNo());
            infoList.add(vo.getSupplyerName());
            infoList.add(vo.getContractNo());
            infoList.add(vo.getCostSubtotal());
            infoList.add(vo.getAccessoriesCost());
            infoList.add(vo.getEndPlace());
            infoList.add(vo.getUsd_rate());
            infoList.add(vo.getRmb_rate());
            data.add(infoList);
        }
    }
    /**
     * 费用数据处理
     * @param Costs
     * @return 处理后的外销客户信息
     */
    private List<Cost> getContractCustomerInfo(List<Cost> Costs){
        return Costs;
    }
    /**
     * 产品数据处理
     * @param Prodects 外销客户信息
     * @return 处理后的外销客户信息
     */
    private List<Product> getContractCustomerInfo1(List<Product> Prodects){
        return Prodects;
    }
    //日期格式转换String
    private String changeDate(Date data){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(data);
        return dateString;
    }
}